package com.wyg.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyg.bean.CarGoods;
import com.wyg.bean.Goods;
import com.wyg.jdbc.Jdbc;

/**
 * Servlet implementation class MsgServlet
 */
@WebServlet("/MsgServlet")
public class MsgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MsgServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String sql="SELECT * FROM usershopcar";
		Jdbc.getConnection();
		ResultSet rs = Jdbc.query(sql);
		List<Object> carlist = new ArrayList<>();
		CarGoods cg =null;
		Float sum = (float) 0;
		try {
			while(rs.next()){
				cg = new CarGoods();
				cg.setXinghao(rs.getString(1));
				Float m = rs.getFloat(2);
				int s = rs.getInt(3);
				sum = sum + s*m;
				cg.setJiage(m);
				cg.setMount(s);
				carlist.add(cg);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		
		request.getSession().setAttribute("carlist",carlist);
		request.getSession().setAttribute("allMoney",sum);
		request.getRequestDispatcher("cart.jsp").forward(request, response);
	}

}
